Ext.define('cceaf.control.storage.SelectCustomerGrid',{
	extend: 'Ext.grid.GridPanel', 
    alias: 'widget.cceafselectcustomergrid',

    /**
	 * 点击触发字段对象
	 */
	triggerField: null,
	
	constructor: function(config){
		config = config || {};
		
		Ext.define('EntityModel', {
	        extend: 'Ext.data.Model',
	        fields: [
     	        {name: 'customerid'}, 
		        {name: 'customerno'},
		        {name: 'customername'}, 
		        {name: 'helpcode'},
		        {name: 'customertype'}, 
		        {name: 'opendate'}
    	   ]
	    });
	    
		//定义数据源
		var ds = new Ext.data.Store({
	    	proxy: {
	    		type: 'ajax',
	    		url: "/" + cceaf.Global.WEB_APP_NAME + '/select/select!query.action?type=CUSTOMER',
	    		reader: {
					type: 'json',
					root: 'data',
					totalProperty: 'totalCount',
					messageProperty: 'message'
				}
			},

	        listeners: {
	        	'load': function(){
	        		var jr = ds.getProxy().getReader();
	        		var message = jr.jsonData.message;
	        		cceaf.util.ExtHelper.showInfoDialog(message);
	        	},
	        	'loadexception': function(){
	        		var jr = ds.getProxy().getReader();
	        		if(jr.jsonData){
		        		var message = jr.jsonData.message;
		        		showErrorDialog(message);
	        		}
	        		else{
	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
	        		}
	        	}
	        },
	        
	        autoDestroy: true,  
	    	pageSize: szpshang.Global.PAGE_SIZE,
	    	model: 'EntityModel'
	    });
	 
		var selModel = new Ext.selection.RowModel();
		
		//查询对象
	    function btnQuery_click(){
	    	var gridPanel = this.up('gridpanel');
	    	
	    	var customerno = gridPanel.down("[name=q_customer_customerno]").getValue();
	    	var customername = gridPanel.down("[name=q_customer_customername]").getValue();
	    	var helpcode = gridPanel.down("[name=q_customer_helpcode]").getValue();
	    	
	    	ds.proxy.extraParams = {
	    			customerno: customerno,
	    			customername: customername,
	    			helpcode: helpcode
		    };
	    	ds.load({params:{isquery:true}});
	    	//Ext.getCmp('selectCustomerGrid').getSelectionModel().selectFirstRow();
	    }
	    
	  	//重置查询条件
	    function btnReset_click(){
	    	var gridPanel = this.up('gridpanel');
	    	
	    	gridPanel.down("[name=q_customer_customerno]").setValue("");
	    	gridPanel.down("[name=q_customer_customername]").setValue("");
	    	gridPanel.down("[name=q_customer_helpcode]").setValue("");
	    }
	    
	 	//定义表格列
	    var columns = [
	    	{xtype: 'rownumberer'},
	        {dataIndex: 'customerid',        header: '客户ID',      sortable: true,  hidden: true}, 
	        {dataIndex: 'customerno',      header: '客户编号',      sortable: true},
	        {dataIndex: 'customername',       header: '客户名称',   sortable: true},
	        {dataIndex: 'helpcode',  header: '助记码',      sortable: true},
	        {dataIndex: 'customertype',      header: '客户类型', sortable: true, renderer: cceaf.Global.cceafCodeRenderer('KHLX')}, 
	        {dataIndex: 'opendate',     header: '开户日期',      sortable: true}
	    ];
	    
		config = Ext.apply(config || {}, {
	    	title:'客户信息',
	        border: true,
	        frame: true,
	        store: ds,
	        columns: columns,
	        selModel: selModel,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        
	        viewConfig: {
				forceFit:true,
				enableRowBody:true,
				getRowClass : function(record, rowIndex, p, ds){
					return '';
				}
			},
	        
			dockedItems:[{
				xtype : 'toolbar',  
                dock : 'top',  
                items : [
					'客户编号',
					{xtype: 'textfield',name: 'q_customer_customerno',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
					'客户名称：',{xtype: 'textfield',name: 'q_customer_customername',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
					'助记码：',{xtype: 'textfield',name: 'q_customer_helpcode',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
					{xtype: "tbfill"},
					{iconCls: 'page_queryIcon', text: ' 查 询 ', pressed: true, handler: btnQuery_click},
					'-',
					{iconCls: 'page_resetIcon', text: ' 重 置 ', pressed: true, handler: btnReset_click}	
				]
			},{
				xtype: 'pagingtoolbar',
				store: ds,
                dock: 'bottom',  
                displayInfo: true,
                displayMsg:'当前显示 {0} - {1}条记录 /共 {2}条记录',
	            emptyMsg:'无显示数据'
			}],
			
			listeners: {
	        	'render': function(){
	        		//tbarButton.render(queryGrid.tbar);
	        	},
	        	'itemdblclick': function(grid,rowIndex,e) {
	        		this.selectRow();
	        	},
	        	'keydown': function(e){
	        		if(e.keyCode == 13 && this.getSelectionModel().getSelected() != null){
	        			this.selectRow();
	        		}
	        	}
	        },
	        
	        selectRow: function(){
				var selectedRow = this.getSelectionModel().getLastSelected();
				
				var triggerField = this.triggerField;
				var triggerFieldInPanel = triggerField.up('panel');
				
				//填充数据到父窗口的控件
				triggerField.setValue(selectedRow.get("customername"));
				if(triggerField.nameOfCustomerid != null && triggerField.nameOfCustomerid != ""){
        			triggerFieldInPanel.down('[name=' + triggerField.nameOfCustomerid + ']').setValue(selectedRow.get("customerid"));
        		}
				if(triggerField.nameOfCustomerno != null && triggerField.nameOfCustomerno != ""){
        			triggerFieldInPanel.down('[name=' + triggerField.nameOfCustomerno + ']').setValue(selectedRow.get("customerno"));
        		}
				if(triggerField.nameOfHelpcode != null && triggerField.nameOfHelpcode != ""){
        			triggerFieldInPanel.down('[name=' + triggerField.nameOfHelpcode + ']').setValue(selectedRow.get("helpcode"));
        		}
        		
        		var selectCustomerWin = Ext.getCmp('_selectCustomerWin');
		    	if(selectCustomerWin){
		    		selectCustomerWin.hide();
		    	}
		    	
		    	//foucobj = getNextControl();
		    	//alert(foucobj.name);
    			//focusObj(foucobj);
	        }
		});

		cceaf.control.storage.SelectCustomerGrid.superclass.constructor.apply(this, arguments);
	}
});